<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <script src="../js/datetime_utils.js"></script>

    <style>
        .grid {
            margin: 100px auto;
            width: 1000px;
            text-align: center;
        }

        .grid table {
            width: 100%;
            border-collapse: collapse;
        }

        .grid th {
            background-color: orange;

        }

        .grid th,td {
            border: 1px solid orange;
            height: 35px;
            line-height: 35px;

        }

        .grid .dept_input {
            margin-bottom: 5px;
            padding-top: 15px;
            background-color: pink;

        }
    </style>


</head>
<body>
<div id="div1">
   <div class="grid">
    <h2>部门管理</h2>
    <div class="dept_input">
        <div>
            编号：
            <input type="text" id="id1" v-model="id" :disabled="flag">
            名称：
            <input type="text" id="name1" v-model="name">
            <button @click="handle" :disabled="submitControl">保存</button>
        </div>
    </div>
       <div class="numtotal">
           <span>当前图书的数量</span>
           <span>{{deptNumTotal}}</span>
       </div>
       <table>
           <tr>
               <th>部门编号</th>    <!--th表示表头-->
               <th>部门名称</th>
               <th>创建时间</th>
               <th>操作</th>
           </tr>

               <!--遍历数据-->
                <tr v-for="item in departments">
                    <td>{{item.id}}</td>                <!--td表示具体元素-->
                    <td>{{item.name}}</td>
                    <td>{{item.date | format('yyyy-MM-dd hh:MM:ss')}}</td>
                    <td>
                        <a href="" @click.prevent="edit(item.id)">修改</a>
                        <span>|</span>
                        <a href="" @click.prevent="deleteDept(item.id)">删除</a>
                    </td>
                </tr>

       </table>
   </div>

</div>

<script>

    Vue.filter('format', function (value,format) {     //全局的过滤器
        console.log(value);
        console.log(format);
        return dateFormat(value,format);
    });

    /*
      动态处理响应式数据

    */
    const vueObj = new Vue({
        el: '#div1',
        data: {
            flag: false,
            id: "",
            name: "",
            submitControl: false,  //控制提交按钮
            departments: [],



        },
        methods: {
            handle: function () {
                //通过flag判断当前是添加还是修改
                if (this.flag) {
                    //是修改
                    this.departments.some((item) => {  //some方法遍历，如果返回为true，立即终止
                        if (item.id == this.id) {
                            item.name = this.name;
                            return true;   //终止循环
                        }
                    });
                    //修改完成把标记设置成false
                    this.flag = false;
                } else {
                    //添加数据

                    const dept = {};   //创建一个对象
                    dept.id = this.id;
                    dept.name = this.name;
                    dept.date = new Date();
                    this.departments.push(dept);
                }
                //重置输入数据
                this.id = '';
                this.name = '';
            },
            edit: function (id) {
                //不能修改编号
                this.flag = true;
                //根据传递过来的id查询要编辑的数据
                const current_dept = this.departments.filter(function (item) {
                    return item.id == id;   //将符合条件的放到filter专门的数组中
                });
                this.id = current_dept[0].id;    //是departments的过滤，所以返回的是数组
                this.name = current_dept[0].name;
            },

            deleteDept: function (id) {
                this.departments = this.departments.filter(function (item) {
                    return item.id != id;    //用于过滤，item.id==id的全部是true，!= 的是false，是true的被加到filter创建的新数组中，false相当于被删了
                });
            },
        },
        computed: {
            deptNumTotal: function () {
                return this.departments.length;
            },
        },
        watch: {
            name: function (value_name) {   //新修改的值
                const flag = this.departments.some(function (item) {    //some函数进行遍历，item是当前遍历到的元素
                    return item.name == value_name;    //有一样的返回true，没有一样的返回false
                });
                if (flag) {
                    //有部门名称相同的
                    this.submitControl = true;
                } else {
                    this.submitControl = false;
                }
            },

        },

        mounted: function () {
            //从后端通过Ajax加载数据
            const data = [
                {
                    id: 1,
                    name: "采购部",
                    date: "2022-02-12",
                },
                {
                    id: 2,
                    name: "销售部",
                    date: "2022-02-12",
                },
                {
                    id: 3,
                    name: "技术部",
                    date: "2022-02-12",
                },
                {
                    id: 4,
                    name: "摸鱼部",
                    date: "2022-02-12",
                }];
            this.departments = data;
        },



    });




</script>
</body>
</html>
